﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Gmail.Presenters;
using Gmail.Presenters.Events;

namespace Gmail.Views
{
    public partial class EmailForm : Form, IEmailView
    {
        public EmailForm()
        {
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(OnFormClosing);
        }

        #region IEmailView Members

        public event EmailSubmitEventHandler EmailSubmitEvent;

        public event ReloginEventHandler ReloginEvent;

        public void HideForm()
        {
            Hide();
        }

        private Boolean _HasDisplayed = false;
        public void InitForm(String Username, IDictionary<String,String> EmailList, String DisplayFilename)
        {
            List<KeyValuePair<String, String>> DS = new List<KeyValuePair<string, string>>(EmailList);
            CmbEmail.DataSource = DS;
            CmbEmail.DisplayMember="Value";
            CmbEmail.ValueMember="Key";

            Text = "Email " + DisplayFilename;

            LblUsername.Text = Username;
            TxtSubject.Text = "";
            TxtBody.Text = "";
            if (!_HasDisplayed)
            {
                _HasDisplayed = true;
                ShowDialog();
            }
            else
            {
                Show();
            }
        }

      

        #endregion

        void OnFormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                HideForm();
                e.Cancel = true;
            }
        }

        private void BtnSend_Click(object sender, EventArgs e)
        {
            String Email = CmbEmail.SelectedValue.ToString();
            String Body = TxtBody.Text;
            String Subject = TxtSubject.Text;
            Cursor ThisCursor = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;
            Boolean Result = EmailSubmitEvent(this, new EmailSubmitEventArgs(Email,Subject,Body));
            Cursor.Current=ThisCursor;
            HideForm();
        }

        private void BtnSwitch_Click(object sender, EventArgs e)
        {
            ReloginEvent(this, new EventArgs());
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            HideForm();
        }
    }
}
